<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片主体区域 -->

    <el-card class="box-card">
      <!-- 按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary" plain @click="tianjiafenlei">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 第三方树形插件 -->
      <el-row>
        <zk-table
          index-text="#"
          :data="cuncateList"
          :columns="columns"
          border
          show-header
          show-row-hover
          :show-index="true"
          :tree-type="true"
          is-fold
          :expand-type="false"
          :selection-type="false"
        >
          <!-- 是否有效区域， 设置对应的模板列： slot="isok"(与columns中设置的template一致) -->
          <template slot="isok" slot-scope="scope">
            <i
              class="el-icon-success"
              v-if="scope.row.cat_deleted === false"
              style="color:lightgreen"
            ></i>
            <i class="el-icon-error" v-else style="color:red"></i>
          </template>

          <!-- 排序 -->
          <template slot="order" slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
            <el-tag size="mini" type="warning" v-else>三级</el-tag>
          </template>

          <!-- 操作 -->
          <template slot="opt" slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="shanchufenlei(scope.row)"
            >删除</el-button>
          </template>
        </zk-table>
      </el-row>

      <!-- 分页区域 -->
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 5, 10, 15]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
        ></el-pagination>
      </el-row>
    </el-card>

    <!-- 添加分类数据的弹出层 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="50%" @close="clecrClose">
      <el-form :rules="rules" label-width="100px" ref="addFormRef" :model="FleiFrom">
        <!-- 添加分类输入框 -->
        <el-form-item v-model="FleiFrom" label="分类名称" prop="catName">
          <el-input v-model="FleiFrom.cat_name"></el-input>
        </el-form-item>
        <!-- 级联选择器框 -->
        <el-form-item label="父级分类">
          <el-cascader
            expandTrigger="hover"
            v-model="selectedCateKeys"
            :options="jiList"
            :props="cateProps"
            @change="handleChange"
            clearable
            change-on-select
            class="widthcase"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="clecrClose">取 消</el-button>
        <el-button type="primary" @click="tianjiafenlei1">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


        
     
 


<script>
export default {
  data() {
    return {
      // 获取商品列表携带参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //   存放获取到的商品列表的数组
      cuncateList: [],
      //   存放获取到的全部数据总和
      total: 0,

      // 树形结构colums配置项
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: '',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          prop: '',

          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          prop: '',
          type: 'template',
          template: 'opt'
        }
      ],
      //   添加分类弹出层默认不显示
      dialogVisible: false,

      //   定义添加分类表单的验证规则
      rules: {
        catName: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ]
      },
      //   添加分类数据框数据
      FleiFrom: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      //   级联选择器数据
      jiList: [],
      //用户在级联下拉菜单中选中的分类id
      selectedCateKeys: [],
      //配置级联菜单中数据如何展示
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  created() {
    //    获取商品列表数据
    this.getcateList()
  },
  methods: {
    // 获取商品列表数据
    async getcateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取商品列表失败')
      this.$message.success('获取商品列表成功')
      console.log(res)
      this.cuncateList = res.data.result
      this.total = res.data.total
    },
    //   分页每页条数回调
    handleSizeChange(newpagesize) {
      console.log(newpagesize)
      this.queryInfo.pagesize = newpagesize
      this.getcateList()
    },
    //   分页当前页数据
    handleCurrentChange(newpagenum) {
      console.log(newpagenum)
      this.queryInfo.pagenum = newpagenum
      this.getcateList()
    },
    // 添加分类函数
    tianjiafenlei() {
      this.dialogVisible = true
      this.getfuji()
      // console.log(111)
    },
    // 获取父级数据列表
    async getfuji() {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取父级列表列表失败')
      this.$message.success('获取父级列表成功')
      console.log(res.data)
      this.jiList = res.data
    },
    // 关闭添加分类弹出层修改数据
    clecrClose() {
      console.log(this.$refs.addFormRef)
      //   this.$refs.addFormRef.resetFields()
      this.$refs.addFormRef.resetFields()
      this.FleiFrom.cat_name = ''
      this.selectedCateKeys = []

      console.log(111)

      this.dialogVisible = false
    },
    // 级联选择器发生变化时把数据传给表单
    handleChange(level) {
      console.log(level)

      if (level.length > 0) {
        this.FleiFrom.cat_pid = level[level.length - 1]
        this.FleiFrom.cat_level = level.length
        // console.log(this.FleiFrom.at_level);

        console.log(this.FleiFrom)
        // console.log(this.FleiFrom.cat_pid)

        return
      } else {
        ;(this.FleiFrom.cat_pid = 0), (this.FleiFrom.cat_level = 0)
        console.log(this.FleiFrom)
      }
    },
    // 点击确定添加分类
    async tianjiafenlei1() {
      console.log(this.FleiFrom)

      const { data: res } = await this.$http.post('categories', this.FleiFrom)
      console.log(res)

      if (res.meta.status !== 201) return this.$message.error('创建分类失败')
      this.$message.success('创建分类成功成功')
      this.dialogVisible = false
      this.getcateList()
    },
    // 点击删除删除分类
    async shanchufenlei(id) {
      console.log(id);
      
      const { data: res } = await this.$http.delete('categories/' + id.cat_id)
      if (res.meta.status !== 200) return this.$message.error('删除分类失败')
      this.$message.success('删除分类成功')
      this.getcateList()
      console.log(res)
    }
  }
}
</script>

<style lang="less" scoped>
.widthcase {
  width: 100%;
}
</style>